using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using MacomberMap.Common.User_Interfaces.Components;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using MacomberMap.Common.Components;
using MacomberMap.Common.Types;
using System.Data.Common;
using MacomberMap.Common.Database_Interface;

namespace MacomberMap.Common.User_Interfaces.One_Lines.Elements
{
    /// <summary>
    /// This class provides a viewable line
    /// </summary>
    public class MM_OneLine_Line : MM_OneLine_Element
    {
        #region Initialization
        /// <summary>
        /// Initialize a new viewable line
        /// </summary>
        /// <param name="DisplayParameters">The display parameters for the line</param>
        public MM_OneLine_Line(MM_OneLine_Display DisplayParameters)
            : base(DisplayParameters)
        {
        }


        /// <summary>
        /// Initialize a new viewable line
        /// </summary>
        /// <param name="ElemConfig">The Xml configuration for the line</param>
        /// <param name="DisplayParameters">The display parameters for the line</param>
        /// <param name="DbConnection">The database connection</param>
        public MM_OneLine_Line(XmlElement ElemConfig, MM_OneLine_Display DisplayParameters, MM_Database DbConnection)
            : base(ElemConfig, DisplayParameters, DbConnection)
        {
            //Retrieve our arrow direction from the 
            Orientation = ParseOrientation(ElemConfig.Attributes["Orientation"].Value);            
        }

        /// <summary>
        /// Initialize a new viewable line
        /// </summary>
        /// <param name="SelectedElement">The CIM element upon which the line is based</param>
        /// <param name="DisplayParameters">The display parameters</param>
        /// <param name="Orientation">The orientation of the line</param>
        public MM_OneLine_Line(MM_Element SelectedElement, MM_OneLine_Display DisplayParameters, enumOrientations Orientation): base(SelectedElement, DisplayParameters, Orientation)
        {            
        }
        

        #endregion

        #region Rendering
        /// <summary>
        /// Draw the line image
        /// </summary>
        /// <param name="g"></param>
        public override void DrawImage(Graphics g)
        {
            Point CenterBounds = CenterRect(DisplayRectangle);            
            Pen ThisPen = KVLevel != null ? KVLevel.Energized.ForePen : Pens.Red;
            if (Orientation == enumOrientations.Up)
            {
                g.DrawLine(ThisPen, CenterBounds.X, CenterBounds.Y, CenterBounds.X, Height - 1);
                g.FillPolygon(ThisPen.Brush, IntToPoints(0, CenterBounds.Y, Width - 1, CenterBounds.Y, CenterBounds.X, 0, 0, CenterBounds.Y));
            }
            else if (Orientation == enumOrientations.Down)
            {
                g.DrawLine(ThisPen, CenterBounds.X, 0, CenterBounds.X, CenterBounds.Y);
                g.FillPolygon(ThisPen.Brush, IntToPoints(0, CenterBounds.Y, Width - 1, CenterBounds.Y, CenterBounds.X, Height - 1, 0, CenterBounds.Y));
            }
            else if (Orientation == enumOrientations.Left)
            {
                g.DrawLine(ThisPen, CenterBounds.X, CenterBounds.Y, Width - 1, CenterBounds.Y);
                g.FillPolygon(ThisPen.Brush, IntToPoints(CenterBounds.X, 0, CenterBounds.X, Height - 1, 0, CenterBounds.Y, CenterBounds.X, 0));
            }
            else if (Orientation == enumOrientations.Right)
            {
                g.DrawLine(ThisPen, 0, CenterBounds.Y, CenterBounds.X, CenterBounds.Y);
                g.FillPolygon(ThisPen.Brush, IntToPoints(CenterBounds.X, 0, CenterBounds.X, Height - 1, Width - 1, CenterBounds.Y, CenterBounds.X, 0));
            }
            else if (Orientation == enumOrientations.Horizontal || Orientation == enumOrientations.Unknown)
            {
                g.FillPolygon(ThisPen.Brush, IntToPoints(0, 0, 0, Height - 1, CenterBounds.X, CenterBounds.Y, 0, 0));
                g.FillPolygon(ThisPen.Brush, IntToPoints(Width - 1, 0, Width - 1, Height - 1, CenterBounds.X, CenterBounds.Y, Width - 1, 0));
                g.DrawLine(ThisPen, Width - 1, 0, Width - 1, Height - 1);
            }
            else if (Orientation == enumOrientations.Vertical)
            {
                g.FillPolygon(ThisPen.Brush, IntToPoints(0, 0, Width - 1, 0, CenterBounds.X, CenterBounds.Y, 0, 0));
                g.FillPolygon(ThisPen.Brush, IntToPoints(0, Height - 1, Width - 1, Height - 1, CenterBounds.X, CenterBounds.Y, 0, Height - 1));
                g.DrawLine(ThisPen, 0, Height - 1, Width - 1, Height - 1);
            }
        }
        /// <summary>
        /// Draw the line text
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TextLabel_Paint(object sender, PaintEventArgs e)
        {
            if (DisplayParameters.Names)
            {
                MM_Line BaseLine = this.BaseElement as MM_Line;
                if (BaseLine == null)
                    return;
                MM_Substation BaseStation = DisplayParameters.BaseElement as MM_Substation;
                if (BaseStation == null)                    
                    e.Graphics.DrawString(BaseLine.Substation1.LongName + "\nto" + BaseLine.Substation2.LongName + "\n(" + BaseLine.Name + ")", SystemFonts.CaptionFont, Brushes.Aqua, PointF.Empty);
                else if (BaseStation == BaseLine.Substation1)
                    e.Graphics.DrawString(BaseLine.Substation2.LongName + "\n(" + BaseLine.Name + ")", SystemFonts.CaptionFont, Brushes.Aqua, PointF.Empty);
                else
                    e.Graphics.DrawString(BaseLine.Substation1.LongName + "\n(" + BaseLine.Name + ")", SystemFonts.CaptionFont, Brushes.Aqua, PointF.Empty);

            }
            //throw new Exception("The method or operation is not implemented.");
        }
        #endregion
    }
}


